/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.data.ui.swing;

import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Administrator
 */
public abstract class AbstractDTOTableModel<T> extends AbstractTableModel {
    String[] columnNames;
    protected List<T> objs;

    public AbstractDTOTableModel() {
        objs = new ArrayList<T>();
        columnNames=getColumnNames();
    }

    public abstract String[] getColumnNames();

    @Override
    public int getRowCount() {
        return objs.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }


    public void add(T p){
        objs.add(p);
        this.fireTableRowsInserted(objs.size()-1, objs.size()-1);
    }

    public void removeRow(int index){
        objs.remove(index);
        this.fireTableRowsDeleted(index, index);
    }

    public T getObj(int index){
        return objs.get(index);
    }

    public void remove(T obj) {
        removeRow(objs.indexOf(obj));
    }
    
    public void remove(List<T> objects){
    	objs.removeAll(objects);
    	fireTableDataChanged();
    }
    
	public void resert(List<T> object) {
		if(object==null)
			objs = new ArrayList<T>();
		else
			objs=object;
		this.fireTableDataChanged();
	}
    
	public void setObject(int row, T obj) {
		objs.set(row, obj);
	}
	
	public List<T> getObjs(){
		return objs;
	}
    
}
